﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Interactivity;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.ComponentModel;
using System.Collections;

namespace XWord.WPF.Behaviors
{
    public class LayoutBehavior : Behavior<FrameworkElement>
    {
        protected override void OnAttached()
        {
            AssociatedObject.Loaded += new RoutedEventHandler(AssociatedObject_Loaded);
           
            base.OnAttached();
        }

        void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
        {
            Panel panel = AssociatedObject as Panel;
            if (panel != null) SetChildProperty(panel.Children);
            ItemsControl itemsControl = AssociatedObject as ItemsControl;
            if (itemsControl != null) SetChildProperty(itemsControl.Items);
            AssociatedObject.Loaded-= AssociatedObject_Loaded;
        }

        private void SetChildProperty(IEnumerable Children)
        {
            //foreach (UIElement element in Children)
            //{
            //    foreach (Setter setter in Setters)
            //    {
            //        if(IsInner)
            //            ((element as ContentControl).Content as FrameworkElement).SetValue(setter.Property, setter.Value);
            //        else
            //        element.SetValue(setter.Property,setter.Value);
            //    }
            //}
        }

        public bool IsInner { get; set; }

        private SetterBaseCollection _setters;

        public SetterBaseCollection Setters
        {
            get
            {
                if (_setters == null) _setters = new SetterBaseCollection();
                return _setters;
            }
        }
    }
}
